package com.xiaotu.spider.platformHome.pageprocessor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.HomeRecommendModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 IQYHomeRecommendPageProcessor
 * @日期 2020年2月27日
 * @作者 wyl
 * @功能 爱奇艺网站首页推荐位
 */
@Component("IQYHomeRecommendPageProcessor")
@Scope("prototype")
public class IQYHomeRecommendPageProcessor extends AUrlPageProcessor{

	private static final String DIANSHIJU_URL = "http://www.iqiyi.com/dianshiju/?vfrm=pcw_home&vfrmblk=C&vfrmrst=712211_channel_dianshiju";
   
	@Override
    protected String[] getStartRequestUrls(){
    	return new String[]{"https://www.iqiyi.com/"};
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
        if (url.contains("dianshiju")){
        	return Constants.PageDataType.TV_RECORD;
        }else{
        	return Constants.PageDataType.SEARCH_LIST;
        }
    }

    @Override
    public void process(Page page){
        String dataType = this.getPageDataType(page);
        switch (dataType)
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page);
                break;
            case Constants.PageDataType.TV_RECORD:
                this.tvRecordProcess(page);
                break;
            default:
                break;
        }
    }
    
    //爱奇艺首页推荐
    private void searchListProcess(Page page){
    	List<HomeRecommendModel> homeCarouselList = new ArrayList<HomeRecommendModel>();
        //首页推荐位轮播
        List<String> homeCarouselHTML = page.getHtml()
                .xpath("//div[@class='focus-side-inner']/ul[@class='side-list']/li").all();
        for (int i = 0; i < homeCarouselHTML.size(); i++) {
        	Html homeCarouselHtml = new Html(homeCarouselHTML.get(i));
			String name = homeCarouselHtml.xpath("//a/span/text()").get();
			homeCarouselList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "首页轮播"));
		}
        
        //首页网络电影
        String homeNetMovieHTML = page.getHtml()
                .xpath("//div[@id='block-M']").get();
        homeNetMovieHTML = homeNetMovieHTML.substring(homeNetMovieHTML.indexOf("qiyiProduced"), 
        		homeNetMovieHTML.lastIndexOf("recommendation"));
        String[] homeNetMovieArr = homeNetMovieHTML.split("recommendation");
        for (int i = 0; i < homeNetMovieArr.length; i++) {
        	String name = homeNetMovieArr[i].substring(homeNetMovieArr[i].indexOf("shortTitle")+10, homeNetMovieArr[i].length());
        	name = name.replace("&", "").replace("quot", "")
        			.replace(";", "").replace(":", "").replace(",", "");
        	homeCarouselList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "首页网络电影"));
			
		}
        
        //首页电视剧
        String homeTVHTML = page.getHtml()
                .xpath("//div[@id='block-O']").get();
        homeTVHTML = homeTVHTML.substring(homeTVHTML.indexOf("isSource"), 
        		homeTVHTML.lastIndexOf("exclusive"));
        String[] homeTVArr = homeTVHTML.split("exclusive");
        for (int i = 0; i < homeTVArr.length; i++) {
        	String name = homeTVArr[i].substring(homeTVArr[i].lastIndexOf("shortTitle")+10, homeTVArr[i].length());
        	name = name.replace("&", "").replace("quot", "")
        			.replace(";", "").replace(":", "").replace(",", "");
        	homeCarouselList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "首页电视剧"));
		}
        
        page.putField(HomeRecommendModel.class.getName(), homeCarouselList);
        PageUtils.addTargetRequest(page, DIANSHIJU_URL, page.getRequest().getExtras());
    }

    //爱奇艺电视剧首页推荐
    private void tvRecordProcess(Page page){
    	List<HomeRecommendModel> homeTVList = new ArrayList<HomeRecommendModel>();
        //电视剧轮播
        List<String> homeTVHTML = page.getHtml()
                .xpath("//div[@class='focus_title focus_width']/a").all();
        for (int i = 0; i < homeTVHTML.size(); i++) {
        	Html homeTVHtml = new Html(homeTVHTML.get(i));
			String name = homeTVHtml.xpath("//a/div[@class='caption']/text()").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "电视剧轮播"));
		}
        
        //同步热播
        List<String> homeSynchHitHTML = page.getHtml()
                .xpath("//div[@id='block-F']/div[@slot='qyModList']/ul/li").all();
        for (int i = 0; i < homeSynchHitHTML.size(); i++) {
        	Html homeSynchHitHtml = new Html(homeSynchHitHTML.get(i));
			String name = homeSynchHitHtml.xpath("//li/div[@class='qy-mod-img vertical']"
					+ "/div[@class='qy-mod-link-wrap']/a/@title").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "同步热播"));
		}
        
    	//爱奇艺自制剧
        List<String> homeSelfMadeTVHTML = page.getHtml()
                .xpath("//div[@id='block-G']/div[@class='dn']/a").all();
        for (int i = 0; i < homeSelfMadeTVHTML.size(); i++) {
        	Html homeSelfMadeTVHtml = new Html(homeSelfMadeTVHTML.get(i));
			String name = homeSelfMadeTVHtml.xpath("a/text()").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "自制剧"));
		}
    	 //热门网剧
    	 List<String> homeHitNetTVHTML = page.getHtml()
                 .xpath("//div[@id='block-H']/div[@class='dn']/a").all();
         for (int i = 0; i < homeHitNetTVHTML.size(); i++) {
         	Html homeHitNetTVHtml = new Html(homeHitNetTVHTML.get(i));
 			String name = homeHitNetTVHtml.xpath("a/text()").get();
 			homeTVList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "热门网剧"));
 		}
    	
    	//即将上映
   	 	List<String> homeComingSoonTVHTML = page.getHtml()
                .xpath("//div[@id='block-J']/div[@class='dn']/a").all();
        for (int i = 0; i < homeComingSoonTVHTML.size(); i++) {
        	Html homeComingSoonTVHtml = new Html(homeComingSoonTVHTML.get(i));
			String name = homeComingSoonTVHtml.xpath("a/text()").get();
			homeTVList.add(new HomeRecommendModel(i+1, name, "爱奇艺", "即将上映"));
		}
        
        page.putField(HomeRecommendModel.class.getName(), homeTVList);
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.IQY_HOME_RECOMMEND;
    }
    
}
